package com.eftimoff.ohosplayer.actions.property;

import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

import java.lang.ref.WeakReference;

class ViewPropertyAnimatorICS extends ViewPropertyAnimator {
    /**
     * A WeakReference holding the native implementation of ViewPropertyAnimator
     */
    private final WeakReference<AnimatorProperty> mNative;

    private final Component view;

    /**
     * A value to be returned when the WeakReference holding the native implementations
     * returns <code>null</code>
     */
    private final static long RETURN_WHEN_NULL = -1L;

    ViewPropertyAnimatorICS(Component view) {
        mNative = new WeakReference<AnimatorProperty>(new AnimatorProperty(view));
        this.view = view;
    }

    @Override
    public ViewPropertyAnimator setDuration(long duration) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.setDuration(duration);
        }
        return this;
    }

    @Override
    public long getDuration() {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            return animatorProperty.getDuration();
        }
        return RETURN_WHEN_NULL;
    }

    @Override
    public ViewPropertyAnimator setStartDelay(long startDelay) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.setDelay(startDelay);
        }
        return this;
    }

    @Override
    public long getStartDelay() {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            return animatorProperty.getDelay();
        }
        return RETURN_WHEN_NULL;
    }

    @Override
    public ViewPropertyAnimator setInterpolator(int interpolator) {
        AnimatorProperty n = mNative.get();
        if (n != null) {
            n.setCurveType(interpolator);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator setListener(final Animator.AnimatorListener listener) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            if (listener == null) {
                animatorProperty.setStateChangedListener(null);
            } else {
                animatorProperty.setStateChangedListener(new ohos.agp.animation.Animator.StateChangedListener() {
                    @Override
                    public void onStart(ohos.agp.animation.Animator animator) {
                    }

                    @Override
                    public void onStop(ohos.agp.animation.Animator animator) {
                    }

                    @Override
                    public void onCancel(ohos.agp.animation.Animator animator) {
                    }

                    @Override
                    public void onEnd(ohos.agp.animation.Animator animator) {
                        listener.onAnimationEnd(null);
                    }

                    @Override
                    public void onPause(ohos.agp.animation.Animator animator) {
                    }

                    @Override
                    public void onResume(ohos.agp.animation.Animator animator) {
                    }
                });
            }
        }
        return this;
    }

    @Override
    public void start() {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            AnimatorGroup mAnimatorSet = new AnimatorGroup();
            mAnimatorSet.setDelay(animatorProperty.getDelay());
            animatorProperty.scaleX(1).scaleY(1).alpha(1);
            mAnimatorSet.runParallel(
                    animatorProperty
            );
            mAnimatorSet.setDuration(animatorProperty.getDuration());
            mAnimatorSet.setCurveType(animatorProperty.getCurveType());
            mAnimatorSet.setDelay(animatorProperty.getDelay());
            mAnimatorSet.start();
        }
    }

    @Override
    public void cancel() {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.cancel();
        }
    }

    @Override
    public ViewPropertyAnimator valueX(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleX(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator xBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleXBy(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator valueY(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleY(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator yBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleYBy(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator rotation(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setRotation(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator rotationBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setRotation(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator rotationX(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setRotation(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator rotationXBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setRotation(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator rotationY(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setRotation(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator rotationYBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setRotation(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator translationX(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setTranslationX(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator translationXBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setTranslationX(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator translationY(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setTranslationY(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator translationYBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            view.setTranslationY(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator scaleX(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleX(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator scaleXBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleXBy(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator scaleY(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleY(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator scaleYBy(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.scaleYBy(value);
        }
        return this;
    }

    @Override
    public ViewPropertyAnimator alpha(float value) {
        AnimatorProperty animatorProperty = mNative.get();
        if (animatorProperty != null) {
            animatorProperty.alpha(value);
        }
        return this;
    }

}
